package com.atguigu.test

import scala.io.Source

object Test2 {
  def main(args: Array[String]): Unit = {
    //读取文件
    val allprovince = Source.fromFile("datas//allprovince.txt").getLines().toList
    val product = Source.fromFile("datas//product.txt").getLines().toList
   // answer1(allprovince,product)
    //answer2(product)
    answer3(product)
  }
  //1、统计哪些省份没有农产品市场
  def answer1(allprovince:List[String],product: List[String]): Unit = {
      //1)过滤脏数据
      val filterlist = product.filter(line => {
        line.split("\t").length == 6
      })
      //2)列裁剪
      val list1 = filterlist.map(x => {
        x.split("\t")(4)
      })
      //3)去重
      val distinctlist = list1.distinct
      //4)差值
      val result = allprovince.diff(distinctlist)
      //5.展示
    result.foreach(println)
  }
  //2、统计农产品种类数最多的三个省份
  def answer2(product: List[String])={
      //1.过滤脏数据
      product.filter(line=>line.split("\t").length==6)
      //2.列裁剪(省份,农产品名称)
        .map(line=>{
          val arr = line.split("\t")
          val province = arr(4)
          val name = arr(0)
          (province,name)
        })
      //3.去重
        .distinct
      //4.分组
        .groupBy {
          case (province,name)=>province
        }
        /*
        map{
          广东->list((广东,大蒜),(广东,葱))
        }
         */
      //5.统计
        .map(tuple=>{
          (tuple._1,tuple._2.length)
        }).toList
        /*
        map{
          广东->4
        }*/
      //6.排序
        .sortBy{
          case (province,num)=>num
        }
      //7.取前三
        .reverse.take(3)
      //8.展示
        .foreach(println)
}
//3、统计每个省份农产品种类数最多的三个农贸市场
    def answer3(product:List[String])={
      //1.过滤
      product.filter(line=>{
        line.split("\t").length==6
      })
      //2.列裁剪(省,农产品名称,农贸市场)
        .map(line=>{
          val arr = line.split("\t")
          val name = arr(0)
          val market = arr(3)
          val province = arr(4)
          (province,market,name)
        })
            //list((province,market,name),(province,market,name),(province,market,name))
      //3.去重
        .distinct
      //4.省份农贸市场分组
          /*map(
             (浙江,农贸1)=>list((浙江,农贸1,香菜),(浙江,农贸1,大白菜),(浙江,农贸1,小白菜))
      */
        .groupBy{
          case (province,market,name) =>(province,market)
        }
      //5.省份农贸市场统计
        /**map(
             (浙江,农贸1,3) ,(浙江,农贸2,4)
         */
        .map(x=>{
          (x._1._1,x._1._2,x._2.length)
        })
      //6.省份分组
        .groupBy{
          case (province,market,num)=>province
        }
        /*
            map(
             浙江=>list((浙江,农贸1,3),(浙江,农贸2,4),(浙江,农贸3,5))
         */
      //7.排序
        .map {
          case (province,list)=>
            (province,list.toList.sortBy(_._3).reverse.take(3))
        }.foreach(println)
      }








}
